package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.entity.PageResult;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;
    //查询品牌列表
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    //根据id查询品牌信息
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    //新增
    @Transactional
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    //修改
    @Transactional
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    //删除
    @Transactional
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    //根据条件查询
    public List<Brand> search(Map<String, Object> searchmap) {
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchmap!=null){
            //模糊查询
            if (searchmap.get("name")!=null && !"".equals(searchmap.get("name"))){
                criteria.andLike("name","%"+searchmap.get("name")+"%");
            }
            //精确查询
            if (searchmap.get("letter")!=null && !"".equals(searchmap.get("letter"))){
                criteria.andEqualTo("letter",searchmap.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }

    //分页查询
    public PageResult findPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<Brand> brands= (Page<Brand>) brandMapper.selectAll();

        long total = brands.getTotal();
        List<Brand> rows = brands.getResult();
        return new PageResult(total,rows);
    }

    //根据条件分页查询
    public PageResult findByCondition(Map<String, Object> map, Integer page, Integer size) {
        PageHelper.startPage(page,size);

        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (map!=null){
            //模糊查询
            if (!"".equals(map.get("name")) && map.get("name")!=null){
                criteria.andLike("name","%"+map.get("name")+"%");
            }
            //精确查询
            if (map.get("letter")!=null && !"".equals(map.get("letter"))){
                criteria.andEqualTo("letter",map.get("letter"));
            }
        }
        //这里要强转为Page<Brand>
        Page<Brand> brands = (Page<Brand>) brandMapper.selectByExample(example);

        long total = brands.getTotal();
        List<Brand> rows = brands.getResult();
        return new PageResult(total,rows);
    }

    //根据分类名称查询品牌指定字段列表(涉及三张表tb_brand，tb_category,tb_brand_category)多对多关系，通过中间关系表连接
    public List<Map> findListByCategoryName(String categoryName) {

        return brandMapper.findListByCategoryName(categoryName);
    }


}